using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using WorkflowProcesses = Scenario.WorkflowEngine.DataModel.WorkflowProcesses;
using WorkflowRelevantData = Scenario.WorkflowEngine.DataModel.WorkflowRelevantData;

using WAPI = Scenario.WAPI;

namespace Scenario.Modeler
{
	/// <summary>
	/// Summary description for FormStartProcess.
	/// </summary>
	public class FormStartProcess : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnOk;
		private System.Windows.Forms.Label lblProcess;
		private System.Windows.Forms.ComboBox ddlProcessTemplate;
		private System.Windows.Forms.ListView listAttributes;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtProcessID;
		private System.Windows.Forms.Button btnCreateProcess;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;


		private WorkflowProcesses.XMLWorkflow xmlWorkflow = null;

		public FormStartProcess()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.panel2 = new System.Windows.Forms.Panel();
            this.btnCancel = new System.Windows.Forms.Button();
            this.btnOk = new System.Windows.Forms.Button();
            this.lblProcess = new System.Windows.Forms.Label();
            this.ddlProcessTemplate = new System.Windows.Forms.ComboBox();
            this.listAttributes = new System.Windows.Forms.ListView();
            this.label1 = new System.Windows.Forms.Label();
            this.txtProcessID = new System.Windows.Forms.TextBox();
            this.btnCreateProcess = new System.Windows.Forms.Button();
            this.panel2.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.btnCancel);
            this.panel2.Controls.Add(this.btnOk);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel2.Location = new System.Drawing.Point(0, 451);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(538, 41);
            this.panel2.TabIndex = 16;
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(227, 7);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(60, 35);
            this.btnCancel.TabIndex = 10;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // btnOk
            // 
            this.btnOk.Enabled = false;
            this.btnOk.Location = new System.Drawing.Point(120, 7);
            this.btnOk.Name = "btnOk";
            this.btnOk.Size = new System.Drawing.Size(67, 35);
            this.btnOk.TabIndex = 9;
            this.btnOk.Text = "Start";
            this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
            // 
            // lblProcess
            // 
            this.lblProcess.Location = new System.Drawing.Point(7, 14);
            this.lblProcess.Name = "lblProcess";
            this.lblProcess.Size = new System.Drawing.Size(386, 14);
            this.lblProcess.TabIndex = 17;
            this.lblProcess.Text = "Process to Start";
            // 
            // ddlProcessTemplate
            // 
            this.ddlProcessTemplate.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.ddlProcessTemplate.Location = new System.Drawing.Point(7, 28);
            this.ddlProcessTemplate.MaxDropDownItems = 20;
            this.ddlProcessTemplate.Name = "ddlProcessTemplate";
            this.ddlProcessTemplate.Size = new System.Drawing.Size(433, 21);
            this.ddlProcessTemplate.TabIndex = 18;
            this.ddlProcessTemplate.SelectedIndexChanged += new System.EventHandler(this.ddlProcessTemplate_SelectedIndexChanged);
            // 
            // listAttributes
            // 
            this.listAttributes.Enabled = false;
            this.listAttributes.FullRowSelect = true;
            this.listAttributes.Location = new System.Drawing.Point(13, 104);
            this.listAttributes.Name = "listAttributes";
            this.listAttributes.Size = new System.Drawing.Size(427, 270);
            this.listAttributes.Sorting = System.Windows.Forms.SortOrder.Ascending;
            this.listAttributes.TabIndex = 19;
            this.listAttributes.UseCompatibleStateImageBehavior = false;
            this.listAttributes.View = System.Windows.Forms.View.Details;
            this.listAttributes.DoubleClick += new System.EventHandler(this.listAttributes_DoubleClick);
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(7, 62);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(133, 14);
            this.label1.TabIndex = 20;
            this.label1.Text = "Process ID";
            // 
            // txtProcessID
            // 
            this.txtProcessID.Enabled = false;
            this.txtProcessID.Location = new System.Drawing.Point(7, 76);
            this.txtProcessID.Name = "txtProcessID";
            this.txtProcessID.Size = new System.Drawing.Size(60, 20);
            this.txtProcessID.TabIndex = 21;
            // 
            // btnCreateProcess
            // 
            this.btnCreateProcess.Location = new System.Drawing.Point(73, 76);
            this.btnCreateProcess.Name = "btnCreateProcess";
            this.btnCreateProcess.Size = new System.Drawing.Size(194, 21);
            this.btnCreateProcess.TabIndex = 22;
            this.btnCreateProcess.Text = "Create Process ID";
            this.btnCreateProcess.Click += new System.EventHandler(this.btnCreateProcess_Click);
            // 
            // FormStartProcess
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(538, 492);
            this.Controls.Add(this.btnCreateProcess);
            this.Controls.Add(this.txtProcessID);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.listAttributes);
            this.Controls.Add(this.ddlProcessTemplate);
            this.Controls.Add(this.lblProcess);
            this.Controls.Add(this.panel2);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.Name = "FormStartProcess";
            this.Text = "Start Process";
            this.Load += new System.EventHandler(this.FormStartProcess_Load);
            this.panel2.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void btnOk_Click(object sender, System.EventArgs e)
		{
			long lProcessID = Int32.Parse(this.txtProcessID.Text);

			string sXML = string.Empty;
			
			/*WorkflowRelevantData.XMLAttributes xmlAttributes = new Scenario.WorkflowEngine.DataModel.WorkflowRelevantData.XMLAttributes();

			for ( int i=0; i<this.listAttributes.Items.Count;i++)
			{
				WorkflowRelevantData.XMLAttribute xmlAttribute = new WorkflowRelevantData.XMLAttribute ();
				xmlAttribute.Name = listAttributes.Items[i].SubItems[0].Text;
				xmlAttribute.Value = listAttributes.Items[i].SubItems[1].Text;

				xmlAttributes.Add ( xmlAttribute);

			}

			if ( xmlAttributes.Count > 0) 
			{
				sXML = xmlAttributes.GetXML ();
				sXML = sXML.Replace ("\\", "\\\\");
				WAPI.Process.SetProcessInstanceAttributes(sXML, lProcessID, ref wapiResponse);
			}*/
			
			try
			{
                WAPI.ProcessHelper helper = new WAPI.ProcessHelper();
                helper.StartWorkflowInstance(lProcessID.ToString(), this.xmlWorkflow.XMLAttributes, true);
			}
			catch
			{
				MessageBox.Show ("Impossible to start Process[" + lProcessID + "]. Unknow Error. Probably ProcessTemplate is not well done!");
				return;
			}

			MessageBox.Show ("Process Started correctly. ID[" + lProcessID + "]");
			this.Close();
		}

		private void FormStartProcess_Load(object sender, System.EventArgs e)
		{
			this.BuildGridHeader();

			WorkflowProcesses.XMLWorkflows xmlWorkflows = new WorkflowProcesses.XMLWorkflows();

            WAPI.ProcessHelper helper = new WAPI.ProcessHelper();
            xmlWorkflows = helper.GetWorkflowsTemplate();

			WorkflowProcesses.XMLWorkflow xmlWorkflow = null;
			int iPosIndex = -1;
			for ( int i=0; i<xmlWorkflows.Count;i++)
			{
				xmlWorkflow = (WorkflowProcesses.XMLWorkflow)xmlWorkflows[i];
				iPosIndex = xmlWorkflow.Name.IndexOf (".");

				if ( iPosIndex >=0 ) 
					this.ddlProcessTemplate.Items.Add ( xmlWorkflow.Name.Substring (0,iPosIndex));
				else
					this.ddlProcessTemplate.Items.Add ( xmlWorkflow.Name);

			}
		}

		private void BuildGridHeader()
		{
			listAttributes.Clear();

			listAttributes.Columns.Add("Name",100, HorizontalAlignment.Left);
			listAttributes.Columns.Add ("Value", listAttributes.Width - 250, HorizontalAlignment.Left);
		}

		private void ddlProcessTemplate_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			this.txtProcessID.Text = string.Empty;
			this.btnOk.Enabled = false;
			this.listAttributes.Enabled = false;
		}

		private void btnCreateProcess_Click(object sender, System.EventArgs e)
		{
			if ( this.ddlProcessTemplate.SelectedIndex<0 ) 
			{
				MessageBox.Show ("A Process selection is mandatory");
				return;
			}

			this.listAttributes.Items.Clear();


            WAPI.ProcessHelper helper = new WAPI.ProcessHelper();
            this.xmlWorkflow = helper.CreateWorkflowInstance(this.ddlProcessTemplate.Text);

			//
            this.txtProcessID.Text = this.xmlWorkflow.ProcessId;

			foreach ( WorkflowRelevantData.XMLAttribute xmlAttribute in this.xmlWorkflow.XMLAttributes)
			{
				ListViewItem item = new ListViewItem( new string[] { xmlAttribute.Name, xmlAttribute.Value});
					
				listAttributes.Items.Add (item);
			}

			this.listAttributes.Enabled = true;

			this.btnOk.Enabled = true;

		}

		private void listAttributes_DoubleClick(object sender, System.EventArgs e)
		{
			if ( listAttributes.SelectedItems.Count == 0 )
			{
				return;
			}

			WorkflowRelevantData.XMLAttribute xmlAttribute = this.xmlWorkflow.XMLAttributes.Get (this.listAttributes.SelectedItems[0].SubItems[0].Text);
			/*xmlAttribute.Name = listAttributes.SelectedItems[0].SubItems[0].Text;
			xmlAttribute.Value = listAttributes.SelectedItems[0].SubItems[1].Text;
			xmlAttribute.Type = "";
			xmlAttribute.Direction = WorkflowRelevantData.XMLAttribute.DIRECTION_NONE;*/

			FormAttribute form = new FormAttribute(xmlAttribute);
			form.ShowDialog (this);

			if ( form.xmlAttribute != null )
			{
				listAttributes.SelectedItems[0].SubItems[0].Text = form.xmlAttribute.Name;
				listAttributes.SelectedItems[0].SubItems[1].Text = form.xmlAttribute.Value;

				xmlAttribute.Value = form.xmlAttribute.Value;
			}
		}
	}
}
